var vm = new Vue({
    el:"#app",
    data(){
        return {
            formItem: {
                Type: '',
                FirstLevel: '',
            },
            FirstLevelList:[],
            secondformItem: {
                FirstLevel: '',
                SecondLevel:'',
            },
            FirstLevelListT:[],
            SecondLevelList:[],
            thirdformItem:{
                FirstLevel: '',
                SecondLevel:'',
                ThirdLevel:'',
            },
            //绑定区域
            BindFirstLevelList:[],
            BindSecondLevelList:[],
            BindThirdLevelList:[],
            bindformItem:{
                FirstLevel: '',
                SecondLevel:'',
                ThirdLevel:'',
                Bm:''
            },
            value:'',
            data1: [],

        }
    },
    created(){
        this.initThisPage();
        this.queryProjectTree();
        this.queryFirstLevel();
    },
    methods:{
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goLogin/initIndexPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        //查询左侧项目树
        queryProjectTree(){
            var self = this;
            $.ajax({
                url: "./goProject/queryProjectTree", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==1){
                        self.data1 = data.TreeJson;
                    }else{
                        toastr.warning("当前没有项目");
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        //查询一级分类
        queryFirstLevel(){
            var self = this;
            $.ajax({
                url: "./goProject/queryFirstLevel", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    var FirstLevelList = new Array();
                    var LevelListJson = data.FirstLevel;
                    for(var i=0;i<LevelListJson.length;i++){
                        var temp = {
                            value: LevelListJson[i].ID,
                            label: LevelListJson[i].title
                        };
                        FirstLevelList.push(temp)
                    }
                    self.FirstLevelList = FirstLevelList;
                    self.FirstLevelListT = FirstLevelList;
                    self.BindFirstLevelList = FirstLevelList;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        //保存一级分类
        saveFirstLevel(){
            var self = this;
            var Type = self.formItem.Type;
            var FirstLevel = self.formItem.FirstLevel;
            if(Type==null||Type==""){
                toastr.warning("请选择分类");
                return false;
            }
            if(FirstLevel==null||FirstLevel.trim()==""){
                toastr.warning("请输入一级分类");
                return false;
            }
            var jsondata = {
                "Type":Type,
                "FirstLevel":FirstLevel
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/saveFirstLevel", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('保存成功');
                        self.formItem.Type = "";
                        self.formItem.FirstLevel = "";
                        self.queryProjectTree();
                        self.queryFirstLevel();
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //查询二级分类
        querySecondLevel(FirstLevel){
            var self = this;
            self.thirdformItem.SecondLevel = "";
            self.thirdformItem.ThirdLevel = "";
            var jsondata = {
                "ID":FirstLevel
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/querySecondLevel", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                type: "POST", //请求方式
                data:data_json,
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    var SecondLevelList = new Array();
                    var LevelListJson = data.SecondLevel;
                    for(var i=0;i<LevelListJson.length;i++){
                        var temp = {
                            value: LevelListJson[i].ID,
                            label: LevelListJson[i].title
                        };
                        SecondLevelList.push(temp)
                    }
                    self.SecondLevelList = SecondLevelList;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        //保存二级分类
        saveSecondLevel(){
            var self = this;
            var ParentID = self.secondformItem.FirstLevel;
            var SecondLevel = self.secondformItem.SecondLevel;
            if(ParentID==null||ParentID==""){
                toastr.warning("请选择一级分类");
                return false;
            }
            if(SecondLevel==null||SecondLevel.trim()==""){
                toastr.warning("请输入二级分类");
                return false;
            }

            var jsondata = {
                "ParentID":ParentID,
                "SecondLevel":SecondLevel
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/saveSecondLevel", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('保存成功');
                        self.secondformItem.FirstLevel = "";
                        self.secondformItem.SecondLevel = "";
                        self.SecondLevelList = [];
                        self.thirdformItem.FirstLevel = "";
                        self.thirdformItem.SecondLevel = "";
                        self.thirdformItem.ThirdLevel = "";
                        self.queryProjectTree();
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //保存三级分类
        saveThirdLevel(){
            var self = this;
            var FirstLevel = self.thirdformItem.FirstLevel;
            var SecondLevel = self.thirdformItem.SecondLevel;
            var ThirdLevel = self.thirdformItem.ThirdLevel;
            if(FirstLevel==null||FirstLevel==""){
                toastr.warning("请选择一级分类");
                return false;
            }
            if(SecondLevel==null||SecondLevel==""){
                toastr.warning("请选择二级分类");
                return false;
            }
            if(ThirdLevel==null||ThirdLevel.trim()==""){
                toastr.warning("请输入三级分类");
                return false;
            }
            var jsondata = {
                "ParentID":SecondLevel,
                "SecondLevel":ThirdLevel
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/saveSecondLevel", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('保存成功');
                        self.thirdformItem.FirstLevel = "";
                        self.thirdformItem.SecondLevel = "";
                        self.thirdformItem.ThirdLevel = "";
                        self.SecondLevelList = [];
                        self.queryProjectTree();
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //绑定部门查询二级分类
        queryBindSecondLevel(FirstLevel){
            var self = this;
            self.bindformItem.SecondLevel = "";
            self.BindSecondLevelList = [];
            var jsondata = {
                "ID":FirstLevel
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/querySecondLevel", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                type: "POST", //请求方式
                data:data_json,
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    var SecondLevelList = new Array();
                    var LevelListJson = data.SecondLevel;
                    for(var i=0;i<LevelListJson.length;i++){
                        var temp = {
                            value: LevelListJson[i].ID,
                            label: LevelListJson[i].title
                        };
                        SecondLevelList.push(temp)
                    }
                    self.BindSecondLevelList = SecondLevelList;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        //绑定部门查询三级分类
        queryBindThirdLevel(SecondLevel){
            var self = this;
            self.bindformItem.ThirdLevel = "";
            self.BindThirdLevelList = [];
            var jsondata = {
                "ID":SecondLevel
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/querySecondLevel", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                type: "POST", //请求方式
                data:data_json,
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    var ThirdLevelList = new Array();
                    var LevelListJson = data.SecondLevel;
                    for(var i=0;i<LevelListJson.length;i++){
                        var temp = {
                            value: LevelListJson[i].ID,
                            label: LevelListJson[i].title
                        };
                        ThirdLevelList.push(temp)
                    }
                    self.BindThirdLevelList = ThirdLevelList;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        //为最下级项目绑定部门
        BindBm(){
            var self = this;
            var FirstLevel = self.bindformItem.FirstLevel;
            var SecondLevel = self.bindformItem.SecondLevel;
            var ThirdLevel = self.bindformItem.ThirdLevel;
            var Bm = self.bindformItem.Bm;
            var BindFirstLevelList = self.BindFirstLevelList;
            var BindSecondLevelList = self.BindSecondLevelList;
            var BindThirdLevelList = self.BindThirdLevelList;
            if(FirstLevel==null||FirstLevel==""){
                toastr.warning("请选择一级分类");
                return false;
            }
            if(SecondLevel==null||SecondLevel==""){
                if(BindSecondLevelList.length!=0){
                    toastr.warning("请选择二级分类");
                    return false;
                }
            }
            if(ThirdLevel==null||ThirdLevel==""){
                if(BindThirdLevelList.length!=0){
                    toastr.warning("请选择三级分类");
                    return false;
                }
            }
            if(Bm==null||Bm==""){
                toastr.warning("请选择绑定部门");
                return false;
            }
            var jsondata = {
                "FirstLevel":FirstLevel,
                "SecondLevel":SecondLevel,
                "ThirdLevel":ThirdLevel,
                "Bm":Bm
            };
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/updateProjectBm", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                type: "POST", //请求方式
                data:data_json,
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('设置成功');
                        self.bindformItem.FirstLevel = "";
                        self.bindformItem.SecondLevel = "";
                        self.bindformItem.ThirdLevel = "";
                        self.bindformItem.Bm = "";
                        self.BindSecondLevelList = [];
                        self.BindThirdLevelList = [];
                    }else{
                        toastr.error('设置失败，请刷新页面重试');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        }
    }
})
